package com.be.beadmin.product.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.basicData.entity.User;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.product.dto.input.WorkHoursRequestSimpDto;
import com.be.beadmin.product.dto.output.WorkHoursRequestEx;
import com.be.beadmin.product.service.WorkHoursRequestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author ljh
* @date 2023-04-18
**/
@Api(tags = {"工时补录（请求）管理"})
@RestController
public class WorkHoursRequestController {

    @Autowired
    private  WorkHoursRequestService workHoursRequestService;

    @ApiOperation("获取负责该项目的生产经理")
    @PostMapping(value = "/product/getProjectManufactManager")
    public ResultModel getProjectManufactManager(@RequestParam String projectId)  {
        User manager = workHoursRequestService.getProjectManufactManager(projectId);
        return ResultModel.success(manager) ;
    }

    @ApiOperation("工时补录申请分页信息")
    @PostMapping(value = "/product/getWorkHoursRequestByPage")
    public ResultModel getWorkHoursRequestByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(workHoursRequestService.getWorkHoursRequestByPage(pageEntity)) ;
    }

    @ApiOperation("新增-工时补录（工单填报工时）")
    @PostMapping("/product/addWorkHoursRequest")
    public ResultModel addWorkHoursRequest(@RequestBody WorkHoursRequestSimpDto workHoursRequestSimpDto){
        workHoursRequestService.addWorkHoursRequest(workHoursRequestSimpDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @ApiOperation("新增-实际项目填报工时")
    @PostMapping("/product/addWorkHoursRequestOfProject")
    public ResultModel addWorkHoursRequestOfProject(@RequestBody WorkHoursRequestSimpDto workHoursRequestSimpDto){
        workHoursRequestService.addWorkHoursRequestOfProject(workHoursRequestSimpDto);
        return ResultModel.success(ResultModel.ADD);
    }


//    @ApiOperation("删除工时补录")
//    @PostMapping("/product/delWorkHoursRequest")
//    public ResultModel delWorkHoursRequest(@RequestBody WorkHoursRequestDto workHoursRequestDto){
//        workHoursRequestService.delWorkHoursRequest(workHoursRequestDto.getId());
//        return ResultModel.success(ResultModel.DELETE);
//    }
//
//    @ApiOperation("编辑工时补录")
//    @PostMapping("/product/updateWorkHoursRequest")
//    public ResultModel updateWorkHoursRequest(@RequestBody WorkHoursRequestDto workHoursRequestDto){
//        workHoursRequestService.updateWorkHoursRequest(workHoursRequestDto);
//        return ResultModel.success(ResultModel.UPDATE);
//    }


    @PostMapping("/product/exportWorkHoursRequest")
    public void exportWorkHoursRequest(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<WorkHoursRequestEx> workHoursRequestPage = workHoursRequestService.getWorkHoursRequestByPage(pageEntity);
        List<WorkHoursRequestEx> workHoursRequestExes = workHoursRequestPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("WorkHoursRequest接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), WorkHoursRequestEx.class).sheet("sheet1").doWrite(workHoursRequestExes);
    }
}